package com.swz.dcrm.ui.viewmodel;

import androidx.lifecycle.MediatorLiveData;

import com.google.gson.Gson;

import com.swz.dcrm.api.DcrmApi;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Clue;
import com.swz.dcrm.model.ClueRecord;
import com.swz.dcrm.model.Dictionary;
import com.swz.dcrm.model.Page;
import com.swz.dcrm.model.PageResponse;
import com.swz.dcrm.model.PersonalInfo;
import com.swz.dcrm.model.Schedule;
import com.swz.dcrm.model.SuggestLevel;
import com.swz.dcrm.model.VisitRecord;
import com.swz.dcrm.model.VisitRecordAndSchedule;
import com.swz.dcrm.util.Constant;
import com.swz.dcrm.util.Tool;
import com.xh.baselibrary.callback.BaseViewModel;
import com.xh.baselibrary.callback.CallBack;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Retrofit;

public class CustomerManagementViewModel extends BaseViewModel {
    private Retrofit mRetrofit;
    private DcrmApi dcrmApi;
    private MediatorLiveData<BaseResponse<Page<ClueRecord>>> potentialClueList;
    private MediatorLiveData<BaseResponse<Page<ClueRecord>>> orderClueList;
    private MediatorLiveData<BaseResponse<Page<ClueRecord>>> defeatedClueList;
    private MediatorLiveData<BaseResponse<VisitRecord>> updateVisitRecord;
    private MediatorLiveData<BaseResponse<List<Dictionary>>>  clueFollowType;
    private MediatorLiveData<BaseResponse<List<Dictionary>>>  cluePhoneResult;
    private MediatorLiveData<BaseResponse<List<Dictionary>>>  clueContactResult;
    private MediatorLiveData<BaseResponse<List<Dictionary>>>clueFollowPurpose;
    private MediatorLiveData<BaseResponse<List<Dictionary>>> clueCustomerLevel;

    private MediatorLiveData<BaseResponse<List<Dictionary>>> carPurchase;
    private MediatorLiveData<BaseResponse<List<Dictionary>>> buyCarTime;
    private MediatorLiveData<BaseResponse<List<Dictionary>>> buyCarFocus;
    private MediatorLiveData<BaseResponse<VisitRecord>> addVisitRecord;
    private MediatorLiveData<BaseResponse<Clue>> addClue;
    private MediatorLiveData<BaseResponse<SuggestLevel>>suggestLevel;
    private MediatorLiveData<BaseResponse<Page<Clue>>> usableClue;
    private MediatorLiveData<PageResponse<PersonalInfo>>saleMan;
    private MediatorLiveData<BaseResponse<String>>authorized;
    private MediatorLiveData<BaseResponse<List<ClueRecord>>>authorizedClueList;




    public CustomerManagementViewModel(Retrofit retrofit){
        this.mRetrofit=retrofit;
        dcrmApi= mRetrofit.create(DcrmApi.class);
    }

    public MediatorLiveData<BaseResponse<List<Dictionary>>> getDictionary(String type){
       switch (type){
           case Constant.DICTIONARY_clueContactResult:
               clueContactResult=creatLiveData(clueContactResult);
               dcrmApi.findDictionaryByTypeCode(type).enqueue(new CallBack<>(this,clueContactResult));
               return clueContactResult;
           case Constant.DICTIONARY_clueFollowPurpose:
               clueFollowPurpose=creatLiveData(clueFollowPurpose);
               dcrmApi.findDictionaryByTypeCode(type).enqueue(new CallBack<>(this,clueFollowPurpose));
               return clueFollowPurpose;
           case Constant.DICTIONARY_clueFollowType:
               clueFollowType=creatLiveData(clueFollowType);
               dcrmApi.findDictionaryByTypeCode(type).enqueue(new CallBack<>(this,clueFollowType));
               return clueFollowType;
           case Constant.DICTIONARY_clueCustomerLevel:
               clueCustomerLevel=creatLiveData(clueCustomerLevel);
               dcrmApi.findDictionaryByTypeCode(type).enqueue(new CallBack<>(this,clueCustomerLevel));
               return clueCustomerLevel;
           case Constant.DICTIONARY_cluePhoneResult:
               cluePhoneResult=creatLiveData(cluePhoneResult);
               dcrmApi.findDictionaryByTypeCode(type).enqueue(new CallBack<>(this,cluePhoneResult));
               return cluePhoneResult;
           case Constant.DICTIONARY_carPurchase:
               carPurchase=creatLiveData(cluePhoneResult);
               dcrmApi.findDictionaryByTypeCode(type).enqueue(new CallBack<>(this,carPurchase));
               return carPurchase;
           case Constant.DICTIONARY_buyCarTime:
               buyCarTime=creatLiveData(buyCarTime);
               dcrmApi.findDictionaryByTypeCode(type).enqueue(new CallBack<>(this,buyCarTime));
               return buyCarTime;
           case Constant.DICTIONARY_buyCarFocus:
               buyCarFocus=creatLiveData(buyCarFocus);
               dcrmApi.findDictionaryByTypeCode(type).enqueue(new CallBack<>(this,buyCarFocus));
               return buyCarFocus;
       }
       return null;
    }


    public MediatorLiveData<BaseResponse<Page<ClueRecord>>> getPotentialClueList(Integer comeShopType,int sort,Integer page,int size){

            potentialClueList=creatLiveData(potentialClueList);
            potentialClueList.setValue(null);
            dcrmApi.potentialClueList(comeShopType,sort,page,size)
                    .enqueue(new CallBack<>(this,potentialClueList));
            return potentialClueList;

    }
    public MediatorLiveData<BaseResponse<Page<ClueRecord>>> getOrderClueList(String orderTime,String deliveryCarTime,int page,int size){
        orderClueList = creatLiveData(orderClueList);
        orderClueList.setValue(null);
        dcrmApi.orderClueList(orderTime, deliveryCarTime, page, size)
                .enqueue(new CallBack<>(this, orderClueList));
        return orderClueList;
    }
    public MediatorLiveData<BaseResponse<Page<ClueRecord>>> getDefeatedClueList(Integer comeShopType,boolean defeatedHasBuyCar,int page,int size){
        defeatedClueList = creatLiveData(defeatedClueList);
        defeatedClueList.setValue(null);
        dcrmApi.defeatedClueList(comeShopType, defeatedHasBuyCar, page, size)
                .enqueue(new CallBack<>(this, defeatedClueList));
        return defeatedClueList;
    }
    public MediatorLiveData<BaseResponse<List<ClueRecord>>>getAuthorizedClueList(){
        authorizedClueList=creatLiveData(authorizedClueList);
        dcrmApi.getAuthorizedClueList().enqueue(new CallBack<>(this,authorizedClueList));
        return authorizedClueList;
    }
    public MediatorLiveData<BaseResponse<VisitRecord>> addRecordAndSchedule(boolean add, VisitRecord visitRecord, Schedule schedule,boolean followFail){
        addVisitRecord=creatLiveData(addVisitRecord);
        VisitRecordAndSchedule visitRecordAndSchedule=new VisitRecordAndSchedule();
        visitRecordAndSchedule.setBsClueRecord(visitRecord);
        visitRecordAndSchedule.setAddDaySchedule(add);
        visitRecordAndSchedule.setFollowFail(followFail);

        if(add){
            visitRecordAndSchedule.setDaySchedule(schedule);
        }
        addVisitRecord.setValue(null);
        dcrmApi.addRecordAndSchedule(visitRecordAndSchedule)
                .enqueue(new CallBack<>(this,addVisitRecord));
        return addVisitRecord;
    }

    public MediatorLiveData<BaseResponse<Clue>> addClue(Clue clue){
        addClue=creatLiveData(addClue);
        addClue.setValue(null);
        dcrmApi.addClue(clue)
                .enqueue(new CallBack<>(this,addClue));
        return addClue;
    }
    public MediatorLiveData<BaseResponse<Clue>> updateClue(Clue clue){
        addClue=creatLiveData(addClue);
        if(addClue.getValue()!=null){
            addClue.setValue(null);
        }
        dcrmApi.updateClue(clue)
                .enqueue(new CallBack<>(this,addClue));
        return addClue;
    }

    public MediatorLiveData<BaseResponse<VisitRecord>> updateVisitRecord(VisitRecord visitRecord) {
        updateVisitRecord=creatLiveData(updateVisitRecord);
        if(updateVisitRecord.getValue()==null){
            updateVisitRecord.setValue(null);
        }
        dcrmApi.updateVisitRecord(visitRecord)
                .enqueue(new CallBack<>(this,updateVisitRecord));
        return updateVisitRecord;
    }

    public MediatorLiveData<BaseResponse<SuggestLevel>> getSuggestLevel(long clueId,int phoneResult,int contactResult) {
        suggestLevel=creatLiveData(suggestLevel);
        suggestLevel.setValue(null);
        dcrmApi.getSuggestLevel(clueId,phoneResult,contactResult).enqueue(new CallBack<>(this,suggestLevel));
        return suggestLevel;
    }

    public MediatorLiveData<BaseResponse<Page<Clue>>> getUsableClue(int page,int size) {
        usableClue=creatLiveData(usableClue);
        dcrmApi.getUsableClue(page,size).enqueue(new CallBack<>(this,usableClue));
        return usableClue;
    }

    public MediatorLiveData<PageResponse<PersonalInfo>> getSaleMan(int page,int size) {
        saleMan=creatLiveData(saleMan);
        dcrmApi.getSaleMan(page,size).enqueue(new CallBack<>(this,saleMan));
        return saleMan;
    }

    public MediatorLiveData<BaseResponse<String>> authorized(long clueId,long userId) {
        authorized=creatLiveData(authorized);
        dcrmApi.authorized(clueId,userId).enqueue(new CallBack<>(this,authorized));
        return authorized;
    }
}
